package cn.wuqingna.legendgem.gem

import cn.wuqingna.legendgem.loader.GemLoader
import cn.wuqingna.legendgem.random.RandomData
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import taboolib.module.chat.colored
import taboolib.module.configuration.util.asMap
import taboolib.module.nms.ItemTagData
import taboolib.module.nms.ItemTagType.*
import taboolib.module.nms.getItemTag

object GemPlant {
    fun buildGem(player: Player, gem: Gem): ItemStack {

        val config = gem.item
        val key = config.name


        // 将需要缓存的数据 存入脚本中
        val random = RandomData(player, gem.cache)

        val item = ItemStack(Material.getMaterial(config.getString("type")), 1, config.getInt("data").toShort())
        val mate = item.itemMeta

        mate.displayName = config.getString("display-name").strScript(random).colored()
        mate.lore = random.scriptStr(config.getStringList("lore")).colored()

        item.itemMeta = mate

        val tag = item.getItemTag()
        tag.putDeep("legendGem.gem-key", key)
        tag.putDeep("legendGem.cache", random.cache)
        tag.saveTo(item)


        return item
    }

    fun buildGem(player: Player, cacheGem: CacheGem): ItemStack {
        val gem = GemLoader.gems[cacheGem.key]!!
        val map = resetMap(cacheGem.map).toMutableMap()

        val random = RandomData(player, map)
        val config = gem.item
        val key = config.name


        // 将需要缓存的数据 存入脚本中

        val item = ItemStack(Material.getMaterial(config.getString("type")), 1, config.getInt("data").toShort())
        val mate = item.itemMeta

        mate.displayName = config.getString("display-name").strScript(random).colored()
        mate.lore = random.scriptStr(config.getStringList("lore")).colored()

        item.itemMeta = mate

        val tag = item.getItemTag()
        tag.putDeep("legendGem.gem-key", key)
        tag.putDeep("legendGem.cache", random.cache)
        tag.saveTo(item)


        return item
    }

    fun String.strScript(randomData: RandomData): String{
        return randomData.scriptStr(this)
    }
    fun resetMap(maps: Map<String, Any?>): Map<String, String> {
        val map = mutableMapOf<String, String>()
        maps.forEach { (t, u) ->
            val tag = GemManager.gson.fromJson(u.toString(), ItemTagData::class.java)
            when (tag.type) {
                STRING -> map[t] = "${tag.asString()}"
                INT -> map[t] = tag.asInt().toString()
                DOUBLE -> map[t] = tag.asDouble().toString()
                END -> {}
                BYTE -> {}
                SHORT -> {}
                LONG -> {}
                FLOAT -> {}
                BYTE_ARRAY -> {}
                LIST -> {}
                COMPOUND -> {}
                INT_ARRAY -> {}
                LONG_ARRAY -> {}
            }
        }
        return map
    }

    fun resetMaps(maps: Map<String, Any?>): Map<String, Any?> {
        val map = mutableMapOf<String, String>()
        maps.forEach { (t, u) ->
            val tag = GemManager.gson.fromJson(u.toString(), ItemTagData::class.java)
            when (tag.type) {
                STRING -> map[t] = tag.asString()
                INT -> map[t] = tag.asInt().toString()
                DOUBLE -> map[t] = tag.asDouble().toString()
                END -> {}
                BYTE -> {}
                SHORT -> {}
                LONG -> {}
                FLOAT -> {}
                BYTE_ARRAY -> {}
                LIST -> {}
                COMPOUND -> {}
                INT_ARRAY -> {}
                LONG_ARRAY -> {}
            }
        }
        return map.asMap()
    }

}